package com.hsurosy.domain.activity.service.trial.node;

import com.hsurosy.domain.activity.model.entity.MarketProductEntity;
import com.hsurosy.domain.activity.model.entity.TrialBalanceEntity;
import com.hsurosy.domain.activity.service.trial.AbstractGroupBuyMarketSupport;
import com.hsurosy.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import com.hsurosy.types.design.framework.tree.StrategyHandler;
import com.hsurosy.types.enums.ResponseCode;
import com.hsurosy.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author Hsu琛君珩
 * @Date 2025-08-11 4:08
 * @Description 根节点
 * @Version: v1.0.0
 */
@Slf4j
@Service
public class RootNode extends AbstractGroupBuyMarketSupport<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> {

    // 开关节点
    @Resource
    private SwitchNode switchNode;

    @Override
    public TrialBalanceEntity doApply(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        // 1. 参数合法性校验
        if (StringUtils.isBlank(requestParameter.getUserId()) ||
                StringUtils.isBlank(requestParameter.getGoodsId()) ||
                StringUtils.isBlank(requestParameter.getSource()) ||
                StringUtils.isBlank(requestParameter.getChannel())) {
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }
        // 2. 通过校验后，路由到下一个节点（switchNode）
        return router(requestParameter, dynamicContext);
    }

    @Override
    public StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> get(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) {
        return switchNode;
    }

}
